<?php
    class Model_schedule extends CI_Model{
        function __construct(){
           
        }

        function open_new_week(){

        }

        function get_detail($datatime){

            $this->db->select('*');
            $this->db->where('datatime',$datatime);
            $query=  $this->db->get('scedule');
            if($query->num_rows($query)>0){
               $row=$query->row();
               return $row;
           } 
        }
        
        function get_weekly_schedule($year,$weeknum){
        
            $weeknum_array=$this->Model_date->get_week_date($year,$weeknum);
            $date_array=explode("-",$weeknum_array[0]);
            $y=(int)$date_array[0];
            $m=(int)$date_array[1];
            $d=(int)$date_array[2];
            $date_array=$this->initialize_date($y,$m,$d);
            $query=$this->db->query($this->get_weekly_schedule_sql($date_array));
            $row = $query->result_array();
                for($i=0;$i<=83;$i++){
                     $result_set[] = ($row[$i]['unit']);
                }
            return $this->sort_schedule($result_set);
        }

        private function sort_schedule($result_unsort){
            for($j=0;$j<=11;$j++){
                for($i=0;$i<84;$i=$i+12){
                $result_set[]=$result_unsort[$i+$j];
                }
            }
            return $result_set;
        }

        function get_weekly_date($year,$weeknum){

            $weeknum_array=$this->Model_date->get_week_date($year,$weeknum);
            $date_array=explode("-",$weeknum_array[0]);

            $y=(int)$date_array[0];
            $m=(int)$date_array[1];
            $d=(int)$date_array[2];

            $date_array=$this->initialize_date($y,$m,$d);

            for($i=0;$i<=6;$i++){
                $weekly_date_array[]=$y.'-'.$m.'-'.($d+$i);
            }
            return  $weekly_date_array ;
        }

        private function get_weekly_schedule_sql($date_array){
            $sql = "SELECT unit FROM wse.scedule WHERE datatime BETWEEN '$date_array[0] 9:00:00' AND '$date_array[6] 20:00:00'";
            return $sql;
        }

        // private function open_new_week_sql($y,$m,$d,$h){

        //         $sql = "INSERT INTO";
        //         $sql .=" wse.scedule(Datatime,Unit,avalible,people_count) VALUES (";
        //         $sql .="'$y-$m-$d $h:00:00'";
        //         $sql .=",NULL,NULL,NULL);";
        //         return $sql; 
        // }
        
        function initialize_date($year,$month,$day){
            if ($month==1||$month==3||$month==5||$month==7||$month==8||$month==10||$month==12) {
                $count_day=31;
            }elseif ($month==4||$month==6||$month==9||$month==11) {
                $count_day=30;
            }elseif ($month==2) {
                //2012 is a leap year
                $count_day=29;
            }

            //across the month
            if($day>$count_day-6){
                $fill=$day+6-$count_day;
                $lastday=0+$fill;
                $count=1;
                for($day;$day<=$count_day;$day++){
                    $day_array[]=$year.'-'.$month.'-'.$day;
                }
                $month++;
                for($day_of_next_month=1;$day_of_next_month<=$fill;$day_of_next_month++){
                    $day_array[]=$year.'-'.$month.'-'.$day_of_next_month;
                }
                return $day_array;
            }

            else{
                $d=$day;
                for($day;$day<=$d+6;$day++){
                    $day_array[]=$year.'-'.$month.'-'.$day;
                }
                return $day_array;
            }
        }
        
    }
?>
